<template>
  <!-- 商品分类导航 -->
  <div class="type-nav">
    <div class="container">
      <div class="nav-left" @mouseenter="isShow = true" @mouseleave="moveOut">
        <h2 class="all">全部商品分类</h2>
        <transition name="category">
          <div class="sort" v-show="isShow">
            <div class="all-sort-list2" @click="goSerach">
              <div
                class="item"
                :class="{ item_on: currentIndex === index }"
                @mouseenter="moveIn(index)"
                @mouseleave="currentIndex = -1"
                v-for="(item1, index) in categoryList"
                :key="item1.categoryId"
              >
                <h3>
                  <a
                    :data-categoryName="item1.categoryName"
                    :data-category1Id="item1.categoryId"
                    >{{ item1.categoryName }}</a
                  >
                </h3>
                <div class="item-list clearfix">
                  <div class="subitem">
                    <dl
                      class="fore"
                      v-for="item2 in item1.categoryChild"
                      :key="item2.categoryId"
                    >
                      <dt>
                        <a
                          :data-categoryName="item2.categoryName"
                          :data-category2Id="item2.categoryId"
                          >{{ item2.categoryName }}</a
                        >
                      </dt>
                      <dd>
                        <em
                          v-for="item3 in item2.categoryChild"
                          :key="item3.categoryId"
                        >
                          <a
                            :data-categoryName="item3.categoryName"
                            :data-category3Id="item3.categoryId"
                            >{{ item3.categoryName }}</a
                          >
                        </em>
                      </dd>
                    </dl>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </transition>
      </div>
      <nav class="nav">
        <a href="#">推荐</a>
        <a href="#">3折疯抢</a>
        <a href="#">唯品快抢</a>
        <a href="#">女装</a>
        <a href="#">运动</a>
        <a href="#">鞋包</a>
        <a href="#">美妆</a>
        <a href="#">电器</a>
        <a href="#">母婴</a>
        <a href="#">男装</a>
        <a href="#">百货</a>
        <a href="#">更多 </a>
      </nav>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
import throttle from "lodash/throttle";
export default {
  name: "TypeNav",
  data() {
    return {
      currentIndex: -1,
      isShow: false,
    };
  },
  mounted() {
    if (this.$route.path !== "/home") {
      this.isShow = false;
    }
  },
  methods: {
    moveIn: throttle(
      function (index) {
        this.currentIndex = index;
      },
      60,
      { trailing: false }
    ),

    // 事件委派完成分类跳转search
    goSerach(e) {
      const { categoryname, category1id, category2id, category3id } =
        e.target.dataset;
      if (categoryname) {
        let location = {
          name: "search",
        };
        location.query = {
          categoryName: categoryname,
          category1Id: category1id,
          category2Id: category2id,
          category3Id: category3id,
        };
        location.params = this.$route.params;
        this.$router.push(location);
      }
    },
    // 离开的回调
    moveOut() {
     
        this.isShow = false;

    },
  },
  computed: {
    // 将vuex中home模块中的state数据映射到当前组件
    ...mapState({
      categoryList: (state) => state.home.cateGoryList,
    }),
  },
};
</script>

<style lang="less" scoped>
.type-nav {
  border-bottom: 2px solid #f10180;

  .container {
    width: 1200px;
    margin: 0 auto;
    display: flex;
    position: relative;

    .all {
      width: 160px;
      height: 45px;
      background-color: RGB(241,1,128,0.7);
      line-height: 45px;
      text-align: center;
      color: #fff;
      font-size: 14px;
      font-weight: bold;
      background-color: #fa2a83;
    }

    .nav {
      a:hover {
        color: #fa2a83;
      }
      a {
        height: 45px;
        margin: 0 22px;
        line-height: 45px;
        font-size: 14px;
        color: #333;
      }
    }

    .sort {
      position: absolute;
      left: 0;
      top: 45px;
      width: 161px;
      height: 483px;
      position: absolute;
      background: #fa2a83;
      z-index: 999;

      .all-sort-list2 {
        .item {
          padding: 6px;
          // &代表父级引用

          &.item_on {
            background-color: #fff;
          }
          h3 {
            &:hover {
              a {
                color: #f10180;
              }
            }
            line-height: 30px;
            font-size: 14px;
            font-weight: 400;
            overflow: hidden;
            padding: 0 20px;
            margin: 0;

            a {
              color: #fff;
            }
          }

          .item-list {
            font-size: 13px;
            margin-left: -50px;
            display: none;
            position: absolute;
            width: 734px;
            height: 483px;
            // min-height: 445x;
            background: #f7f7f7;
            left: 210px;
            border: 1px solid #ddd;
            top: 0;
            z-index: 9999 !important;

            .subitem {
              float: left;
              width: 650px;
              padding: 0 4px 0 8px;

              dl {
                border-top: 1px solid #eee;
                padding: 6px 0;
                overflow: hidden;
                zoom: 1;

                &.fore {
                  border-top: 0;
                }

                dt {
                  float: left;
                  width: 54px;
                  line-height: 22px;
                  text-align: right;
                  padding: 3px 6px 0 0;
                  font-weight: 600;
                }

                dd {
                  float: left;
                  width: 415px;
                  padding: 3px 0 0;
                  overflow: hidden;

                  em {
                    float: left;
                    height: 14px;
                    line-height: 14px;
                    padding: 0 8px;
                    margin-top: 5px;
                    border-left: 1px solid #ccc;
                  }
                }
              }
            }
          }

          &:hover {
            .item-list {
              display: block;
            }
          }
        }
      }

      .nav {
        a:first-child {
          color: red;
        }
        // align-self: center;
        // border: 2px solid blue;
        a {
          font-size: 15px;
          padding: 0 15px;
          height: 43px;
          line-height: 43px;
        }
      }
    }
  }
}

// 移入category加的过渡动画
.category-enter {
  height: 0;
  opacity: 0;
}
.category-enter-active {
  transition: all 1s;
}
.category-enter-to {
  height: 461px;
  opacity: 1;
}

.category-leave {
  height: 461;
  opacity: 1;
}
.category-leave-active {
  transition: all 0.7s;
}
.category-leave-to {
  height: 0;
  opacity: 0;
}
</style>
